package com.qeegoo.b2bautozityp.moudle.detail.adapter;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.qeegoo.b2bautozityp.moudle.detail.bean.DetailBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HongQian.Wang on 2017/10/19.
 */

public class PageAdapter extends android.support.v4.view.PagerAdapter {
          private int leftPosition;  //左边列表的位置

    //存储在售商品与历史报价里面的容器List也是全局唯一的

    private List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> sellingList = new ArrayList<>();
    private List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> historyList = new ArrayList<>();

    private List<String>  selectedList=new ArrayList<String>();  //选中的商品List 全局只有一个
    private List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean>  selectedDataList=new ArrayList<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean>();
    private SellAdapter leftSelladapter;
    private SellAdapter rightSelladapter;
    private String productId;
    private ExpandAdapter expandAdapter;
    private Context context;
    private ListView leftSellListView;
    private ListView rightSellListView;

    public PageAdapter(FragmentActivity activity) {
        this.context = activity;
    }

    public void setExpandAdapter(ExpandAdapter expandAdapter) {
        this.expandAdapter = expandAdapter;
        leftSellListView = new ListView(context);
        rightSellListView = new ListView(context);
        leftSelladapter = new SellAdapter(this, sellingList, expandAdapter,selectedList,selectedDataList);
        rightSelladapter = new SellAdapter(this, historyList, expandAdapter,selectedList,selectedDataList);
        leftSellListView.setAdapter(leftSelladapter);
        rightSellListView.setAdapter(rightSelladapter);
    }
    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        if (position == 0) {
            leftSelladapter.setProductId(productId);
            leftSelladapter.setDataAndRefreshed(leftPosition,sellingList);
            container.addView(leftSellListView);
            return leftSellListView;
        } else {
            rightSelladapter.setProductId(productId);
            rightSelladapter.setDataAndRefreshed(leftPosition,sellingList);
            container.addView(rightSellListView);
            return rightSellListView;
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }


    public void setDataAndRefreshed(int position,DetailBean.GoodsInfoMoreListBean goodsInfoMoreListBean) {
        this.leftPosition=position;
        parseData(goodsInfoMoreListBean);
        notifyDataSetChanged();
    }

    public void parseData(DetailBean.GoodsInfoMoreListBean goodsInfoMoreListBean) {
        sellingList.clear();   //将在售商品与历史报价里面的数据清空
        historyList.clear();
        List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> askPriceQuoteGoodsList = goodsInfoMoreListBean.getAskPriceQuoteGoodsList();
        productId = goodsInfoMoreListBean.getProductId();
        for (DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean bean : askPriceQuoteGoodsList) {
            if (1 == Integer.parseInt(bean.getAssistType())) {
                sellingList.add(bean);
            } else {
                historyList.add(bean);  //添加在售商品与历史商品里面的数据
            }
        }
    }


}
